package com.stjb.furns.service.impl;

import com.stjb.furns.dao.FurnDAO;
import com.stjb.furns.dao.impl.FurnDAOImpl;
import com.stjb.furns.entity.Furn;
import com.stjb.furns.entity.Page;
import com.stjb.furns.service.FurnsService;

import java.util.List;

/**
 * @author situ
 * @version 1.0
 * @date 2025/5/26 21:48
 * @Description:
 */
public class FurnsServiceImpl implements FurnsService {

    private FurnDAO furnDAO = new FurnDAOImpl();

    /**
     * 查询所有
     *
     * @return
     */
    @Override
    public List<Furn> queryFurns() {
        return furnDAO.queryFurns();
    }

    @Override
    public int addFurn(Furn furn) {

        return furnDAO.addFurn(furn);
    }

    /**
     * 根据id删除家具
     *
     * @param id
     * @return 受影响行数
     */
    @Override
    public int deleteFurnById(int id) {
        return furnDAO.delete(id);
    }

    /**
     * 根据id查家居
     *
     * @param id
     * @return
     */
    @Override
    public Furn queryFurnById(int id) {
        return furnDAO.queryFurnById(id);
    }

    /**
     * 根据传入的对象修改家居
     *
     * @param furn
     * @return
     */
    @Override
    public int updateFurn(Furn furn) {
        return furnDAO.updateFurn(furn);
    }

    /**
     * 填充数据
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public Page<Furn> page(int pageNo, int pageSize) {
        //创建page对象 填充数据
        Page<Furn> page = new Page<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        int totalRow = furnDAO.getTotalRow();
        page.setTotalRow(totalRow);
        //计算一共有多少叶
        int pageTotalCount = totalRow / pageSize;
        if (totalRow % pageSize > 0) {
            pageTotalCount += 1;
        }
        page.setPageTotalCount(pageTotalCount);

        //计算分页内容
        int begin = (pageNo - 1) * pageSize;
        List<Furn> pageItems = furnDAO.getPageItems(begin, pageSize);
        page.setItems(pageItems);

        //url

        return page;


    }

    @Override
    public Page<Furn> pageByName(int pageNo, int pageSize, String name) {
        Page<Furn> page = new Page<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        int totalRow = furnDAO.getTotalRowByName(name);
        page.setTotalRow(totalRow);
        //计算一共有多少叶
        int pageTotalCount = totalRow / pageSize;
        if (totalRow % pageSize > 0) {
            pageTotalCount += 1;
        }
        page.setPageTotalCount(pageTotalCount);

        //计算分页内容
        int begin = (pageNo - 1) * pageSize;
        List<Furn> pageItems = furnDAO.grtPageItemByName(begin, pageSize,name);
        page.setItems(pageItems);

        //url

        return page;

    }
}
